package commandline;

import java.util.*;

public class CommandLineParser {
	
	private Map<String, Integer> map;
	private String[] args;
	
	public CommandLineParser(String[] args) {
		this.map = new HashMap<String, Integer>();
		this.args = args;
		for (int i=0;i<args.length;i++)
			this.map.put(args[i], i);
	}
	
	public int contains(String flag) {
		if (flag == null)
			throw new IllegalArgumentException();
		if (map.containsKey(flag))
			return map.get(flag);
		return -1;
	}
	
	public String getTrailingArg(int index) {
		if (index < 0 || index >= args.length)
			throw new IllegalArgumentException();
		return args[index];
	}
	
	public String getTrailingArg(String flag) {
		int temp = contains(flag);
		if (temp >= 0 && temp < args.length - 1)
			return args[temp + 1];
		return null;
	}
	
	public String getArgsString() {
		StringBuilder sb = new StringBuilder();
		for (String s : args) {
			sb.append(s).append(" ");
		}
		return sb.substring(0, sb.length()-1).toString();
	}
}
